import { CellModel } from "./CellModel";
import { GameData } from "./GameData";

export class GameModel {
    constructor() {
        this._initSuccCellIds();
        this._initCellModes();
        this._addSuccCellIds();
        this._addCellModels();

        this.firstCellModel = null;
    }

    _initCellModes() {
        this.cellModels = [];
        for (let y = 0; y < 9; y++) {
            this.cellModels[y] = [];
        }
    }

    _addCellModels() {
        let datas = G.json.getJson(GameData.level + '');
        for (let i = 0; i < datas.length; i++) {
            let cellId = '';
            for (let j = 0; j < datas[i].knotData.length; j++) {
                cellId += datas[i].knotData[j];
            }
            let cellModel = new CellModel(datas[i],cellId);
            let pos = datas[i].pos;
            if(GameData.succMode){
                pos = datas[i].succPos;
            }
            this.cellModels[pos.y][pos.x] = cellModel;
        }
    }

    _initSuccCellIds() {
        this.succCellIds = [];
        for (let y = 0; y < 9; y++) {
            this.succCellIds[y] = [];
        }
    }
    _addSuccCellIds() {
        let datas = G.json.getJson(GameData.level + '');
        for (let i = 0; i < datas.length; i++) {
            let cellId = '';
            for (let j = 0; j < datas[i].knotData.length; j++) {
                cellId += datas[i].knotData[j];
            }
            this.succCellIds[datas[i].succPos.y][datas[i].succPos.x] = cellId;
        }
    }

    getCellModels() {
        return this.cellModels;
    }

    clickCell(pos) {
        let cellModel = this.cellModels[pos.y][pos.x];
        if (cellModel.getIsFixed() == 1) {
            // 固定
            return { code: 0, firstPos: null };
        }

        if (!this.firstCellModel) {
            // 第一个
            this.firstCellModel = cellModel;
            return { code: 1, firstPos: null };
        }

        if (this.firstCellModel == cellModel) {
            // 取消选择
            this.firstCellModel = null;
            return { code: 2, firstPos: null };
        }

        // 交换
        // 交换this.cellModels中的位置
        let firstPos = this.firstCellModel.getPos();
        this.cellModels[pos.y][pos.x] = this.firstCellModel;
        this.cellModels[firstPos.y][firstPos.x] = cellModel;
        // 交换cellModel的pos
        cellModel.pos = firstPos;
        this.firstCellModel.pos = pos;
        this.firstCellModel = null;
        return { code: 3, firstPos: firstPos };
    }

    checkGameOver() {
        let isOver = true;

        for (let y = 0; y < 9; y++) {
            for (let x = 0; x < 11; x++) {
                let cellModel = this.cellModels[y][x];
                if (!cellModel) {
                    continue;
                }
                if (cellModel.getIsFixed() == 1) {
                    continue;
                }
                let succCellId = this.succCellIds[y][x];
                let cellId = cellModel.getCellId();
                if (cellId != succCellId) {
                    isOver = false;
                    break;
                }
            }
        }
        return isOver;
    }
}